package com.ruoyi.common.function;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jdk.nashorn.internal.objects.annotations.Function;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : hcl
 * @Date : 2023/8/10 09:58
 * 分页查询所有数据
 */
@FunctionalInterface
public interface SelectPageAllFunction<T> {

    void results(List<T> list);


    /**
     * @param size           每页查询条数
     * @param selectFunction 执行语句
     */
    default SelectPageAllFunction<T> select(int size, SelectFunction<T> selectFunction) {
        int page = 1;
        List<T> all = null;
        while (true) {
            startPage(page++, size);
            List<T> select = selectFunction.select();
            PageInfo<T> pageInfo = new PageInfo<T>(select);
            if (all == null) {
                Long total = (Long) pageInfo.getTotal();
                all = new ArrayList<T>(total.intValue());
            }
            all.addAll(pageInfo.getList());
            if (pageInfo.isIsLastPage()) {
                break;
            }
        }
        this.results(all);
        return this;
    }

    default void startPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize).setReasonable(true);
    }
}
